// pages/search/search.js
import {network} from "../index/network"
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'searched',
      success:function(res){
        // console.log(res);
        var data = res.data;
        that.setData({
          histories:data
        })
      }
    })
  },

  onSearchInputEvent:function(event){
    // console.log(event);
    var value = event.detail.value;
    var that = this;
    network.getSearch({
      q:value,
      success:function(subjects){
        // console.log(subjects);
        that.setData({
          subjects:subjects
        })            
      }
    })
  },
  onItemTapEvent:function(event){
    var htat = this;
    console.log(event);
    var id = event.currentTarget.dataset.id;
    var title = event.currentTarget.dataset.title;
    var histories = that.data.histories;
    histories.push({id:id,title:title})
    wx.setStorage({
      data: histories,
      key: 'searched',
      success:function(){
        console.log("保存成功");
      }
    })
    wx.navigateTo({
      url:'/pages/detail/detail?type=tv&id='+id,
    })
  }
})